package com.mall.biz.user.record;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.biz.user.VerifyCodeBizImpl;
import com.mall.client.mall.MallService;
import com.mall.client.order.MinsuOrderService;
import com.mall.client.user.UserService;
import com.mall.pojo.MinsuOrder;
import com.mall.pojo.MinsuOrderExample;
import com.mall.pojo.User;
import com.mall.pojo.MinsuOrderExample.Criteria;
import com.mall.query.TradeRecordQuery;
import com.mall.vo.item.MinsuOrderVO;
import com.mall.web.vo.ResponseEntity;
import com.mall.web.vo.StatusInfo;

@Service
public class MinsuRecordBizImpl {
	@Autowired
	private MinsuOrderService minsuOrderService;
	@Autowired
	private MallService  mallService;
	@Autowired
	private UserService userService;
	@Autowired
	private VerifyCodeBizImpl verifyCodeBiz;

	public List<MinsuOrderVO> getMinsuOrderVOListByQuery(TradeRecordQuery tradeRecordQuery) {
		List<MinsuOrder> minsuOrderLsit = getMinsuOrderListByQuery(tradeRecordQuery);
		List<MinsuOrderVO> minsuOrderVOList = new ArrayList<>();
		MinsuOrderVO minsuOrderVO = null;
		MinsuOrder minsuOrder = null;
		for (int i = 0; i < minsuOrderLsit.size(); i++) {
			minsuOrderVO = new MinsuOrderVO();
			minsuOrder = minsuOrderLsit.get(i);
			minsuOrderVO.setMinsuOrder(minsuOrder);
			minsuOrderVO.setMall(this.mallService.getMallById(minsuOrder.getMallId()));
			minsuOrderVO.setUser(this.userService.getUserByUserId(minsuOrder.getSellerId()));//卖家信息
			minsuOrderVOList.add(minsuOrderVO);
		}
		return minsuOrderVOList;
	}

	private List<MinsuOrder> getMinsuOrderListByQuery(TradeRecordQuery tradeRecordQuery) {
		MinsuOrderExample example = new MinsuOrderExample();
		example.setOrderByClause("id desc");
		example.setLimit(tradeRecordQuery.getPage().getLength());
		example.setStart(tradeRecordQuery.getPage().getBegin());
		Criteria cirteria = example.createCriteria();
	
		cirteria.andCommOnlyKeyEqualTo(tradeRecordQuery.getOnlyKey());
		cirteria.andBuyUserIdEqualTo(tradeRecordQuery.getUserId());
		if(tradeRecordQuery.getStatus()!=null){
			cirteria.andStatusEqualTo(tradeRecordQuery.getStatus());
		}
		
		List<MinsuOrder>  minsuOrderList = this.minsuOrderService.getMinsuOrderListByExample(example);
		return minsuOrderList;
	}
	
	
	/**
	 * 民宿用户下单成功后给店主短信提醒
	 * @param minsuOrderId
	 * @return
	 */
	public ResponseEntity<String> payMinsuSuccessCallback(Long minsuOrderId ) {
		try {
			MinsuOrder minsuOrder = this.minsuOrderService.getMinsuOrderById(minsuOrderId);
//			Long userId = trade.getBuyUserId();
			if(minsuOrder == null){
				ResponseEntity<String> responseEntity = new ResponseEntity<String>(StatusInfo.REQUEST_FAILURE,
						StatusInfo.REQUEST_FAILURE_MSG, null);
				return responseEntity;
			}
			
			User seller = this.userService.getUserByUserId(minsuOrder.getSellerId());
			User bookUser = this.userService.getUserByUserId(minsuOrder.getBuyUserId());
			String result = verifyCodeBiz.sendMessToSeller(seller.getPhone().trim(),bookUser.getPhone().trim());// 给用户发送短信验证
			if (StringUtils.isEmpty(result)) {

				ResponseEntity<String> responseEntity = new ResponseEntity<String>(StatusInfo.REQUEST_FAILURE,
						StatusInfo.REQUEST_FAILURE_MSG, null);
				return responseEntity;
			}

			if ("sendmsg error".equals(result)) {
				ResponseEntity<String> responseEntity = new ResponseEntity<String>("-201", "号码发送过于频发", null);
				return responseEntity;
			}
			ResponseEntity<String> responseEntity = new ResponseEntity<String>(StatusInfo.REQUEST_SUCCESS,
					StatusInfo.REQUEST_SUCCESS_MSG, result);
			return responseEntity;
		} catch (Exception e) {

			ResponseEntity<String> responseEntity = new ResponseEntity<String>(StatusInfo.REQUEST_FAILURE,
					StatusInfo.REQUEST_FAILURE_MSG, null);
			return responseEntity;
		}
	}
}
